<?php
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>TOUPROPRE DBMS</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" media="screen" type="text/css" title="Design" href="styles/design.css" />
</head>

<body>
    <!-- TITLE  -->
	<div id="page_title">
      <h1>Stock transfer</h1>
    </div>

    <!-- Side Menues -->
    <?php include "tools/Menues.html"; ?>

    <!-- Main Body -->
    <div id="page_body">
        
<?php
/*
 * Includes
 */
include "tools/Forms.php";
include "tools/Queries.php";

$nitems = 5;

/*
 * Function Definitions:
 */
function ShowInputForm() {
    global $nitems;
    $before = '<br/>Sending Facility ID : <input type="text" name="from" />'.
            '<br/>Receiving Facility ID : <input type="text" name="to" />'.
            '</br/><br/>';
    EchoItemListForm("stock_transfer.php",$nitems,$before,'');
}

/* 
 * Behaviour :
 * 
 * @bugs displays wrong error message when an itemID is unknown
 */
if(isset($_POST['submit'])) {
    if ($_POST['from']=='' or $_POST['to']=='') {
        echo "<p class=sql_error>You must enter a sender and receiver facility.</p>";
    }
    else {
        $from = $_POST['from'];
        $to   = $_POST['to'];
        for($i=0; $i<$nitems; $i++) {
            $itemID = $_POST["item$i"];
            $qty    = $_POST["qty$i"];
            if($itemID!='' AND $qty!='') {
                $ok = WithdrawStockFromFacility($itemID, $qty, $from);
                if($ok) {
                    AddStockToFacility($itemID, $qty, $to);
                    echo "<p class=sql_cmd>Added $qty $itemID in facility $to.</p>";
                }
            }
        }
    }
}
else {
    ShowInputForm();
}

?>
    </div>
    
    <!-- Footer -->
    <?php include "tools/Footer.html"; ?>
</body>
</html>
